<?php
class App
{
	public static $runTime = 0;
	private $view = null;
	
	public function __construct()
	{
		$this->_initAutoLoader();
		$this->_initEnv();
		$this->_initInput();
		$this->_initOutput();
		$this->_initView();
	}
	public function run()
	{
		RegGlobal::set('START_TIME', Functions::microtime_float());
		$this->_initAction();
	}
	
	public function _initEnv()
	{
		date_default_timezone_set('Asia/Shanghai');
		error_reporting(E_ALL);
		if(PHP_VERSION < '5.3.0') 
		{
			set_magic_quotes_runtime(0);
		}
		define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());
	}
	
	private function _initAutoLoader()
	{
		require_once CORE_ROOT . '/controller/abstract.php';
		require_once CORE_ROOT . '/model/abstract.php';
		require_once CORE_ROOT . '/config/interface.php';
		require_once CORE_ROOT . '/config/core.php';
		require_once CORE_ROOT . '/view.php';
		require_once CORE_ROOT . '/db.php';
		require_once CORE_ROOT . '/regglobal.php';
		spl_autoload_register(array($this, 'autoLoader'));
	}
	
	private function _initInput()
	{
		if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) ||  isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS']))
		{
			exit('request_tainting');
		}
		if(MAGIC_QUOTES_GPC) 
		{
			$_GET = Functions::dstripslashes($_GET);
			$_POST = Functions::dstripslashes($_POST);
			$_COOKIE = Functions::dstripslashes($_COOKIE);
		}
	}
	
	private function _initOutput()
	{
		
	}
	
	private function _initView()
	{
		$this->view = new View();
	}
	
	private function _initAction()
	{
		$m = Config::getConfig('CORE_MODULE_NAME');
		$a = Config::getConfig('CORE_ACTION_NAME');
		$defaultModule = Config::getConfig('CORE_DEFAULT_MODULE');
		$defaultAction = Config::getConfig('CORE_DEFAULT_ACTION');
		$defaultModule = empty($defaultModule) ? 'index' : $defaultModule;
		$defaultAction = empty($defaultAction) ? 'index' : $defaultAction;
		
		$moduleName = isset($_GET[$m]) && !empty($_GET[$m]) ? $_GET[$m] : $defaultModule;
		$moduleClass = ucfirst(strtolower($moduleName)).'Controller';
		$actionMothod = isset($_GET[$a]) && !empty($_GET[$a]) ? $_GET[$a] : $defaultAction;

		if(!class_exists($moduleClass))
		{
			exit($moduleClass.':Module \'' . $moduleName . '\' not found!');
		}
		else if(!method_exists($moduleClass, $actionMothod))
		{
			exit('Action \'' . $moduleClass . '->' . $actionMothod . '\' not found!');
		}
		
		$module = new $moduleClass($moduleName, $actionMothod, $this->view);
		$module->$actionMothod();
	}
	
	public function _autoLoader($className)
	{
		$clsname = $className;
		if (false !== strpos($className, '_'))
		{
			$className = str_replace('_', '/', $className);
			$className = DOC_ROOT . "/{$className}";
		}
		if (!file_exists("{$className}.php"))
		{
			return false;
		}
		require("{$className}.php");
	}
	
	public function autoLoader($className)
	{
		$classes = array('Config', 'Functions');
		$file = '';
		if(substr($className, -5) == "Model")
		{
			$name = strtolower(substr($className, 0, -5));
			$file = APP_ROOT.'/model/'.$name.'.php';
		}
		else if(substr($className, -10) == "Controller")
		{
			$name = strtolower(substr($className, 0, -10));
			$file = APP_ROOT.'/controller/'.$name.'.php';
		}
		else if(substr($className, -6) == "Config" && $className != 'Config')
		{
			$name = strtolower(substr($className, 0, -6));
			$file = APP_ROOT.'/config/'.$name.'.php';
		}
		else if(in_array($className, $classes))
		{
			$file = CORE_ROOT.'/'.strtolower($className).'.php';
		}
		else
		{
		    $file = CORE_ROOT.'/lib/'.strtolower($className).'.php';
		    if(!file_exists($file) || !is_file($file))
		    {
		        $file = APP_ROOT.'/lib/'.strtolower($className).'.php';
		    }
		}
		if ($file != '' && file_exists($file) && is_file($file))
		{
			require($file);
			return true;
		}
		return false;
	}
	
	public function __destruct()
	{
		//Core_db_master::close();
		//Core_db_slave::close();
	}
}

function var_dump_pre($var)
{
	echo '<pre>';
	var_dump($var);
	echo '</pre>';
}

function print_r_pre($var)
{
	echo '<pre>';
	print_r($var);
	echo '</pre>';
}